"""
Case Type   : 功能测试
Case Name   : 修改参数max_concurrent_autonomous_transactions值正常
Create At   : 2024/7/3
Owner       : peilinqian
Description :
    step1:设置max_concurrent_autonomous_transactions参数值为0;
    step2:设置max_concurrent_autonomous_transactions参数值为1024;
    step3:设置max_concurrent_autonomous_transactions参数值为500;
Expect      :
    step1:设置max_concurrent_autonomous_transactions参数值为0; expect：设置成功
    step2:设置max_concurrent_autonomous_transactions参数值为1024; expect：设置成功
    step3:设置max_concurrent_autonomous_transactions参数值为500; expect：设置成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class DblinkCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.log.info('----查询参数----')
        self.init_para = self.com.show_param(
            "max_concurrent_autonomous_transactions")

    def test_main(self):
        step_txt = '----step1:设置max_concurrent_autonomous_transactions' \
                   '参数值为0; expect：设置成功----'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"max_concurrent_autonomous_transactions=0", )
        self.assertTrue(msg, '执行失败:' + step_txt)
        self.restart_result = self.pri_sh.restart_db_cluster()
        self.log.info('----查询参数----')
        msg = self.com.show_param("max_concurrent_autonomous_transactions")
        self.assertEqual(msg, '0', '执行失败:' + step_txt)

        step_txt = '----step2:设置max_concurrent_autonomous_transactions' \
                   '参数值为1024; expect：设置成功----; '
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"max_concurrent_autonomous_transactions=1024", )
        self.assertTrue(msg, '执行失败:' + step_txt)
        self.restart_result = self.pri_sh.restart_db_cluster()
        self.log.info('----查询参数----')
        msg = self.com.show_param("max_concurrent_autonomous_transactions")
        self.assertEqual(msg, '1024', '执行失败:' + step_txt)

        step_txt = '----step3:设置max_concurrent_autonomous_transactions' \
                   '参数值为500; expect：设置成功----'
        self.log.info(step_txt)
        sql = f"alter system set " \
              f"max_concurrent_autonomous_transactions=500;" \
              f"select pg_sleep(5);"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.restart_result = self.pri_sh.restart_db_cluster()
        self.log.info('----查询参数----')
        msg = self.com.show_param("max_concurrent_autonomous_transactions")
        self.assertEqual(msg, '500', '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----恢复max_concurrent_autonomous_transactions' \
                   '参数值为小数; expect：设置成功----'
        self.log.info(step_txt)
        msg = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f"max_concurrent_autonomous_transactions={self.init_para}", )
        self.restart_result = self.pri_sh.restart_db_cluster()
        self.log.info('----查询参数----')
        msg1 = self.com.show_param("max_concurrent_autonomous_transactions")

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
        self.assertTrue(msg, '执行失败:' + step_txt)
        self.assertEqual(msg1, self.init_para, '执行失败:' + step_txt)
